unit Class_SORT;

interface
uses
  Classes,SysUtils,DB,Uni,UniEngine,StrUtils;

type
  TSORT=class(TUniEngine)
  private
    FSORTIDEX : Integer;
    FSORTPREV : Integer;
    FSORTCODE : string;
    FSORTLINK : string;
    FSORTTYPE : string;
    FSORTNAME : string;
    FSORTMEMO : string;
    FZCLBLINK : string;
    FBOOLUSAG : Integer;
    FBOOLCREW : Integer;           
    FSORTGSDM : string;
    FSORTYSDM : string;
    FQCJMLINK : string;
    FSORTJLDW : string;
  protected
    procedure SetParameters;override;
    function  GetStrInsert:string;override;
    function  GetStrUpdate:string;override;
    function  GetStrDelete:string;override;
  public
    function  GetStrsIndex:string;override;
  public
    function  GetNextIdex:Integer;overload;
    function  GetNextIdex(AUniConnection:TUniConnection):Integer;overload;
  public
    function  CheckExist(AUniConnection:TUniConnection):Boolean;override;
    procedure PrepareYSDM(ACodeRul:string);
  public
    destructor  Destroy; override;
    constructor Create;  
  published
    property SORTIDEX : Integer read FSORTIDEX  write FSORTIDEX;
    property SORTPREV : Integer read FSORTPREV  write FSORTPREV;
    property SORTCODE : string read FSORTCODE  write FSORTCODE;
    property SORTLINK : string read FSORTLINK  write FSORTLINK;
    property SORTTYPE : string read FSORTTYPE  write FSORTTYPE;
    property SORTNAME : string read FSORTNAME  write FSORTNAME;
    property SORTMEMO : string read FSORTMEMO  write FSORTMEMO;
    property ZCLBLINK : string read FZCLBLINK  write FZCLBLINK;
    property BOOLUSAG : Integer read FBOOLUSAG  write FBOOLUSAG;
    property BOOLCREW : Integer read FBOOLCREW  write FBOOLCREW;
    property SORTGSDM : string read FSORTGSDM  write FSORTGSDM;
    property SORTYSDM : string read FSORTYSDM  write FSORTYSDM;
    property QCJMLINK : string read FQCJMLINK  write FQCJMLINK;
    property SORTJLDW : string read FSORTJLDW  write FSORTJLDW;    
  public
    class function  ReadDS(AUniQuery:TUniQuery):TUniEngine;override;
    class procedure ReadDS(AUniQuery:TUniQuery;var Result:TUniEngine);override;
    class function  CopyIt(ASORT:TSORT):TSORT;overload;        
    class procedure CopyIt(ASORT:TSORT;var Result:TSORT);overload;       
  end;

implementation

uses
  Class_KzDebug;



{ TSORT }
procedure TSORT.SetParameters;
begin
  inherited;
  with FUniSQL.Params do
  begin
    case FOptTyp of
      otAddx:
      begin
        ParamByName('SORT_IDEX').Value := SORTIDEX;
        ParamByName('SORT_PREV').Value := SORTPREV;
        ParamByName('SORT_CODE').Value := SORTCODE;
        ParamByName('SORT_LINK').Value := SORTLINK;
        ParamByName('SORT_TYPE').Value := SORTTYPE;
        ParamByName('SORT_NAME').Value := SORTNAME;
        ParamByName('SORT_MEMO').Value := SORTMEMO;
        ParamByName('ZCLB_LINK').Value := ZCLBLINK;
        ParamByName('BOOL_USAG').Value := BOOLUSAG;
        ParamByName('BOOL_CREW').Value := BOOLCREW;
        ParamByName('SORT_GSDM').Value := SORTGSDM;
        ParamByName('SORT_YSDM').Value := SORTYSDM;
        ParamByName('QCJM_LINK').Value := QCJMLINK;
        ParamByName('SORT_JLDW').Value := SORTJLDW; 
      end;
      otEdit:
      begin
        ParamByName('SORT_IDEX').Value := SORTIDEX;
        ParamByName('SORT_PREV').Value := SORTPREV;
        ParamByName('SORT_CODE').Value := SORTCODE;
        ParamByName('SORT_LINK').Value := SORTLINK;
        ParamByName('SORT_TYPE').Value := SORTTYPE;
        ParamByName('SORT_NAME').Value := SORTNAME;
        ParamByName('SORT_MEMO').Value := SORTMEMO;
        ParamByName('ZCLB_LINK').Value := ZCLBLINK;
        ParamByName('BOOL_USAG').Value := BOOLUSAG;
        ParamByName('BOOL_CREW').Value := BOOLCREW;
        ParamByName('SORT_GSDM').Value := SORTGSDM;
        ParamByName('SORT_YSDM').Value := SORTYSDM;
        ParamByName('QCJM_LINK').Value := QCJMLINK;
        ParamByName('SORT_JLDW').Value := SORTJLDW; 
      end;  
      otDelt:
      begin
        ParamByName('SORT_IDEX').Value := SORTIDEX;
 
      end;  
    end;
  end;
end;

function TSORT.CheckExist(AUniConnection: TUniConnection): Boolean;
begin
  Result:=CheckExist('TBL_SORT',['SORT_IDEX',SORTIDEX],AUniConnection);
end;

function TSORT.GetNextIdex: Integer;
begin

end;

function TSORT.GetNextIdex(AUniConnection: TUniConnection): Integer;
begin

end;

function TSORT.GetStrDelete: string;
begin
  Result:='DELETE FROM %S WHERE   SORT_IDEX=:SORT_IDEX';
  Result:=Format(Result,[TablNam]);
end;

function TSORT.GetStrInsert: string;
begin
  Result:='INSERT INTO %S'
         +'    ( SORT_IDEX, SORT_PREV, SORT_CODE, SORT_LINK, SORT_TYPE'
         +'    , SORT_NAME, SORT_MEMO, ZCLB_LINK, BOOL_USAG, BOOL_CREW'
         +'    , SORT_GSDM, SORT_YSDM, QCJM_LINK, SORT_JLDW)'
         +'    VALUES'
         +'    (:SORT_IDEX,:SORT_PREV,:SORT_CODE,:SORT_LINK,:SORT_TYPE'
         +'    ,:SORT_NAME,:SORT_MEMO,:ZCLB_LINK,:BOOL_USAG,:BOOL_CREW'
         +'    ,:SORT_GSDM,:SORT_YSDM,:QCJM_LINK,:SORT_JLDW)';
  Result:=Format(Result,[TablNam]);
end;

function TSORT.GetStrsIndex: string;
begin
  Result:=Format('%D',[SORTIDEX]);
end;

function TSORT.GetStrUpdate: string;
begin
  Result:='UPDATE  %S SET'
         +'    SORT_PREV=:SORT_PREV,'
         +'    SORT_CODE=:SORT_CODE,'
         +'    SORT_LINK=:SORT_LINK,'
         +'    SORT_TYPE=:SORT_TYPE,'
         +'    SORT_NAME=:SORT_NAME,'
         +'    SORT_MEMO=:SORT_MEMO,'
         +'    ZCLB_LINK=:ZCLB_LINK,'
         +'    BOOL_USAG=:BOOL_USAG,'
         +'    BOOL_CREW=:BOOL_CREW,'
         +'    SORT_GSDM=:SORT_GSDM,'
         +'    SORT_YSDM=:SORT_YSDM,'
         +'    QCJM_LINK=:QCJM_LINK,'
         +'    SORT_JLDW=:SORT_JLDW'
         +'    WHERE SORT_IDEX=:SORT_IDEX';
  Result:=Format(Result,[TablNam]);
end;

constructor TSORT.Create;
begin
  SORTJLDW:='-1';
end;

destructor TSORT.Destroy;
begin

  inherited;
end;

class function TSORT.ReadDS(AUniQuery: TUniQuery): TUniEngine;
var
  I:Integer;
  Field:TField;
  FieldName:string;
begin
  Result:=TSORT.Create;

  with TSORT(Result) do
  begin
    for I:=0 to AUniQuery.Fields.Count-1 do
    begin
      Field:=AUniQuery.Fields.Fields[I];
      //if field.fieldname is not all uppercase,please use uppercase().
      FieldName:=Field.FieldName;
      if FieldName='SORT_IDEX' then
      begin
        SORTIDEX :=Field.AsInteger;
      end else
      if FieldName='SORT_PREV' then
      begin
        SORTPREV :=Field.AsInteger;
      end else
      if FieldName='SORT_CODE' then
      begin
        SORTCODE :=Field.AsString;
      end else
      if FieldName='SORT_LINK' then
      begin
        SORTLINK :=Field.AsString;
      end else
      if FieldName='SORT_TYPE' then
      begin
        SORTTYPE :=Field.AsString;
      end else
      if FieldName='SORT_NAME' then
      begin
        SORTNAME :=Field.AsString;
      end else
      if FieldName='SORT_MEMO' then
      begin
        SORTMEMO :=Field.AsString;
      end else
      if FieldName='ZCLB_LINK' then
      begin
        ZCLBLINK :=Field.AsString;
      end else
      if FieldName='BOOL_USAG' then
      begin
        BOOLUSAG :=Field.AsInteger;
      end else
      if FieldName='BOOL_CREW' then
      begin
        BOOLCREW :=Field.AsInteger;
      end else
      if FieldName='SORT_GSDM' then
      begin
        SORTGSDM :=Field.AsString;
      end else
      if FieldName='SORT_YSDM' then
      begin
        SORTYSDM :=Field.AsString;
      end else
      if FieldName='QCJM_LINK' then
      begin
        QCJMLINK :=Field.AsString;
      end else
      if FieldName='SORT_JLDW' then
      begin
        SORTJLDW :=Field.AsString;
      end;
    end
  end;  
end;

class procedure TSORT.ReadDS(AUniQuery: TUniQuery; var Result: TUniEngine);
var
  I:Integer;
  Field:TField;
  FieldName:string;
begin
  if Result=nil then Exit;

  with TSORT(Result) do
  begin
    for I:=0 to AUniQuery.Fields.Count-1 do
    begin
      Field:=AUniQuery.Fields.Fields[I];
      //if field.fieldname is not all uppercase,please use uppercase().
      FieldName:=Field.FieldName;
      if FieldName='SORT_IDEX' then
      begin
        SORTIDEX :=Field.AsInteger;
      end else
      if FieldName='SORT_PREV' then
      begin
        SORTPREV :=Field.AsInteger;
      end else
      if FieldName='SORT_CODE' then
      begin
        SORTCODE :=Field.AsString;
      end else
      if FieldName='SORT_LINK' then
      begin
        SORTLINK :=Field.AsString;
      end else
      if FieldName='SORT_TYPE' then
      begin
        SORTTYPE :=Field.AsString;
      end else
      if FieldName='SORT_NAME' then
      begin
        SORTNAME :=Field.AsString;
      end else
      if FieldName='SORT_MEMO' then
      begin
        SORTMEMO :=Field.AsString;
      end else
      if FieldName='ZCLB_LINK' then
      begin
        ZCLBLINK :=Field.AsString;
      end else
      if FieldName='BOOL_USAG' then
      begin
        BOOLUSAG :=Field.AsInteger;
      end else
      if FieldName='BOOL_CREW' then
      begin
        BOOLCREW :=Field.AsInteger;
      end else
      if FieldName='SORT_GSDM' then
      begin
        SORTGSDM :=Field.AsString;
      end else
      if FieldName='SORT_YSDM' then
      begin
        SORTYSDM :=Field.AsString;
      end else
      if FieldName='QCJM_LINK' then
      begin
        QCJMLINK :=Field.AsString;
      end else
      if FieldName='SORT_JLDW' then
      begin
        SORTJLDW :=Field.AsString;
      end;
    end
  end;  
end;

class function  TSORT.CopyIt(ASORT: TSORT): TSORT;
begin
  Result:=TSORT.Create;
  TSORT.CopyIt(ASORT,Result)
end;

class procedure TSORT.CopyIt(ASORT:TSORT;var Result:TSORT);
begin
  if Result=nil then Exit;
  Result.SORTIDEX :=ASORT.SORTIDEX;
  Result.SORTPREV :=ASORT.SORTPREV;
  Result.SORTCODE :=ASORT.SORTCODE;
  Result.SORTLINK :=ASORT.SORTLINK;
  Result.SORTTYPE :=ASORT.SORTTYPE;
  Result.SORTNAME :=ASORT.SORTNAME;
  Result.SORTMEMO :=ASORT.SORTMEMO;
  Result.ZCLBLINK :=ASORT.ZCLBLINK;
  Result.BOOLUSAG :=ASORT.BOOLUSAG;
  Result.BOOLCREW :=ASORT.BOOLCREW;
  Result.SORTGSDM :=ASORT.SORTGSDM;
  Result.SORTYSDM :=ASORT.SORTYSDM;
  Result.QCJMLINK :=ASORT.QCJMLINK;
  Result.SORTJLDW :=ASORT.SORTJLDW;
end;

procedure TSORT.PrepareYSDM(ACodeRul:string);
var
  I:Integer;
  TMPA:string;
  TMPB:Integer;
  TMPC:Integer;
  function getLeng:Integer;
  var
    X:Integer;
  begin
    Result:=0;
    for X:=1 to I do
    begin
      Result:=Result+ StrToInt(ACodeRul[X]);
    end;
  end;
begin
  for I:=Length(ACodeRul) downto 1 do
  begin
    TMPB:=getLeng;
    TMPC:=StrToInt(ACodeRul[I]);
    TMPA:=Copy(SORTYSDM,TMPB-TMPC+1,TMPC);
    if TMPA  =  DupeString('0',StrToInt(ACodeRul[I])) then
    begin
      Delete(FSORTYSDM,TMPB-TMPC+1,TMPC);
      KzDebug.FileLog(FSORTYSDM);
    end;
  end;
end;

end.
 
